<template>
    <div class="user-devicecode">
        <el-form :inline="true" :model="formInline">
            <el-form-item label="地区">
                <el-input v-model="formInline.area" placeholder="地区"></el-input>
            </el-form-item>
            <el-form-item label="昵称">
                <el-input v-model="formInline.nickname" placeholder="昵称"></el-input>
            </el-form-item>
            <el-form-item label="手机号码">
                <el-input v-model="formInline.phone" placeholder="手机号码"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="search">搜索</el-button>
                <el-button type="primary" @click="reset">重置</el-button>
            </el-form-item>
        </el-form>
        <el-table
        :header-cell-style="{color:'#555555'}"
        class="black"
        :data="tableData"
        max-height="650"
        stripe
        border
        style="width: 100%">
            <el-table-column
                prop="id"
                label="设备ID"
                align="center">
            </el-table-column>
            <el-table-column
                prop="area"
                label="地区"
                align="center">
            </el-table-column>
            <el-table-column
                prop="nickname"
                label="昵称"
                align="center">
            </el-table-column>
            <el-table-column
                prop="phone"
                label="手机号码"
                align="center">
            </el-table-column>
            <el-table-column
                prop="createTime"
                label="新增时间"
                align="center">
            </el-table-column>
            <el-table-column
                prop="deviceCode"
                label="设备码"
                align="center">
            </el-table-column>
            <el-table-column
                prop="status"
                label="状态"
                align="center">
            </el-table-column>
            <el-table-column label="操作" align="center" width="254">
                <template slot-scope="scope">
                <el-button @click="onEditClick(scope.row)" size="small">封设备</el-button>
                <el-button @click="onDelClick(scope.row)" size="small">解封设备</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="footer-page">
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="page"
                :page-sizes="[10, 20, 50, 100]"
                :page-size="page_size"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total_page">
            </el-pagination>
        </div>
        <el-dialog
        title="提示"
        :visible.sync="editDialogVisible"
        width="30%">
            <span>确认封禁该设备吗？</span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="onSureClick()">确 定</el-button>
            </span>
        </el-dialog>
        <el-dialog
        title="提示"
        :visible.sync="delDialogVisible"
        width="30%">
            <span>确认解封该设备吗？</span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="delDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="onSureClick()">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    name: 'user-devicecode',
    data () {
        return {
            editDialogVisible: false,
            delDialogVisible: false,
            params: {
                page: 1,
                limit: 10,
                order: 'asc'
            },
            page: 1,
            page_size: 10,
            total_page: 0,
            tableData: [],
            state_id: '',
            status: true,
            formInline: {
                area: '',
                nickname: '',
                phone: ''
            }
        }
    },
    created () {

    },
    mounted () {
        let data = {
            'params': this.params
        }
        this.getList(data)
    },
    methods: {
        getList (data) {
            this.tableData = []
            this.$api.user.getDeviceCodeList(data, res => {
                if (res.code === 200) {
                    let data = res.page.list || []
                    for (let i = 0; i < data.length; i++) {
                        this.tableData.push(data[i])
                    }
                    this.total_page = res.page.totalCount
                    this.page_size = res.page.pageSize
                    this.page = res.page.currPage
                }
            })
        },
        search () {
            this.page = 1
            this.params['area'] = this.formInline.area
            this.params['nickname'] = this.formInline.nickname
            this.params['phone'] = this.formInline.phone
            this.getList({ 'params': this.params })
        },
        reset () {
            this.page = 1
            this.formInline.area = ''
            this.formInline.nickname = ''
            this.formInline.phone = ''
            this.params['area'] = this.formInline.area
            this.params['nickname'] = this.formInline.nickname
            this.params['phone'] = this.formInline.phone
            this.getList({ 'params': this.params })
        },
        onEditClick (row) {
            this.editDialogVisible = true
            this.state_id = row.id
            this.status = false
        },
        onDelClick (row) {
            this.delDialogVisible = true
            this.state_id = row.id
            this.status = true
        },
        onSureClick () {
            this.$api.user.editDeviceCode({'id': this.state_id, 'status': this.status}, res => {
                if (res.code === 200) {
                    this.$message.success(res.msg)
                    this.editDialogVisible = false
                    this.delDialogVisible = false
                    this.reset()
                } else {
                    this.$message.error(res.msg)
                }
            })
        },
        handleSizeChange (val) {
            this.page_size = val
            this.params.limit = val
            this.getList({ 'params': this.params })
        },
        handleCurrentChange (val) {
            this.page = val
            this.params.page = val
            this.getList({ 'params': this.params })
        }
    }
}
</script>

<style lang="less" scoped>
    .user-devicecode {
        position: relative;
        width: 100%;
        height: 100%;
        .margin-bottom-12 {
            margin-bottom: 10px;
        }
        .el-row {
            margin-bottom: 20px;
            &:last-child {
                margin-bottom: 0;
            }
        }
        .el-col {
            border-radius: 4px;
        }
        .grid-content {
            position: relative;
            height: 40px;
        }
        .grid-padding {
            /* padding-left: 20px; */
            padding-right: 20px;
        }
        .grid-title {
            float: left;
            width: 72px;
            height: 40px;
            line-height: 40px;
            font-size: 14px;
            color: #333;
            text-align: center;
        }
        .grid-input {
            float: left;
            width: 130px;
            height: 40px;
            line-height: 40px;
            font-size: 14px;
            color: #333;
            text-align: center;
        }
        .row-bg {
            padding: 10px 0;
            background-color: #f9fafc;
        }
        .footer-page {
            width: 540px;
            margin: 0 auto;
            padding-top: 20px;
        }
    }
</style>
